package com.cyzy.config;

import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;

/**
 * 配置websocket里获取http对象
 *
 * @author Honarly
 * @date 2024.11.11
 */
public class GetHttpSessionConfig extends ServerEndpointConfig.Configurator {
    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
        HttpSession session = (HttpSession) request.getHttpSession();
        if (session != null) {
            sec.getUserProperties().put(HttpSession.class.getName(), session);
        } else {
            // 如果会话不存在，可以选择创建一个新的会话
//             session = request.getHttpServletRequest().getSession(true);
//             sec.getUserProperties().put(HttpSession.class.getName(), session);
            // 或者记录日志
            System.out.println("会话不存在");
        }
    }
}
